package com.zuikaku.filter;

import com.zuikaku.pojo.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        User activeUser = (User) request.getSession().getAttribute("activeUser");
        if(activeUser!=null){
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
            HttpServletResponse response =(HttpServletResponse) servletResponse;
            PrintWriter writer = response.getWriter();
            writer.println("未登录");
            writer.flush();
        }

    }

    @Override
    public void destroy() {

    }
}
